<template>
	<div class="system-dept-container">
		<el-card shadow="hover">
			<div class="system-dept-search mb15">
				<el-input v-model="state.tableData.param.companyname" size="default" placeholder="请输入代理商名称" clearable style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="primary" class="ml10" v-auth="'company:add'" @click="onOpenAddDept">
					<el-icon>
						<ele-Plus />
					</el-icon>
					添加
				</el-button>
			</div>
			<el-table
				border
				:data="state.tableData.data"
				style="width: 100%"
				row-key="id"
				v-loading="state.tableData.loading"
			>
				<el-table-column type="index" label="序号" align="center" width="70" />
				<el-table-column prop="companyname" label="代理商名称" align="center" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="companyaddress" label="代理商地址" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="cnum" label="购买PC软件数量" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createAt" label="创建时间" align="center" show-overflow-tooltip>
					<template #default="scope">
						{{ scope.row.createAt && formatDate(new Date(scope.row.createAt), 'YYYY-mm-dd HH:MM:SS') }}
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" show-overflow-tooltip width="150" v-auths="['company:edit','company:del']">
					<template #default="scope">
						<el-button size="small" type="warning" v-auth="'company:edit'" @click="onOpenEditDept(scope.row)">编辑</el-button>
						<el-button size="small" type="danger" v-auth="'company:del'" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页组件 -->
			<Pagination 
				class="mt15"
				:total="state.tableData.total"
				:page="state.tableData.param.pageNum"
				:limit="state.tableData.param.pageSize"
				@pagination="pageChange"
			/>
		</el-card>
		<addCompany ref="addCompanyRef" @refresh="getTableData" />
	</div>
</template>

<script setup lang="ts" name="systemCompany">
import addCompany from './modal/addCompany.vue';
import { formatDate } from '/@/utils/formatTime';
// 后台接口
import { systemApi } from '/@/api/system/index';

const addCompanyRef = ref();
const state = reactive<SysCompanyState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
      companyname: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});
// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	let res: any = await systemApi().getCompanyList(state.tableData.param);
	state.tableData.data = res.datas.list;
	state.tableData.total = res.datas.count;
	state.tableData.loading = false;
};
// 打开新增菜单弹窗
const onOpenAddDept = () => {
	addCompanyRef.value.openDialog();
};
// 打开编辑菜单弹窗
const onOpenEditDept = (row: RowCompanyType) => {
	addCompanyRef.value.openDialog(row);
};
// 删除当前行
const onTabelRowDel = (row: RowCompanyType) => {
	ElMessageBox.confirm(`此操作将永久删除该部门, 是否继续?`, '提示', {
		confirmButtonText: '删除',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			let res: any = await systemApi().delCompany({ id: row.id });
			if (res.success) {
				getTableData();
				ElMessage.success('操作成功');
			} else {
				ElMessage.error('操作失败');
			}
		})
		.catch(() => {});
};
// 分页组件改变
const pageChange = (pageObj: {pageNum:number,pageSize:number}) => {
	state.tableData.param.pageNum = pageObj.pageNum;
	state.tableData.param.pageSize = pageObj.pageSize;
	getTableData();
}
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
